<template>
    <!-- 非余额支付 h5专用 -->
	<view class="container">
		<view class="pay-text">{{ payText }}</view>
	</view>
</template>

<script>
	import { order_result } from '@/api/goods.js';

	export default {
		data() {
			return {
				payText: '支付中...',
				goods_list: []
			};
		},
		onLoad(options) {
			console.log('options-', options);
			const {order_no} = options;
			uni.showLoading({
				title: '',
				mask: true
			});
			order_result({order_no}).then(res => {
				uni.hideLoading();
				const {poolList, probabilityList, blindbox_id} = uni.getStorageSync('orderPayInfo');
				const goods_num = uni.getStorageSync('goods_num');
				const {code, data = {}} = res;
				if (code == 0) {
					const {id, detail = []} = data;
					for (let i = 0; i < detail.length; i++) {
						detail[i].goods_image = detail[i].goods_image.split(',')[0]
					}
					this.goods_list = [...detail];
					this.$store.commit('goods/setPoolsDetails', {poolList, probabilityList});
					// 筛选出赠品
					const guarantRes = detail.filter(item => {
						return item.source == 2;
					});
					if(guarantRes.length) {
						this.$store.commit('goods/setGoodsDetails', {goods_list: this.goods_list, guarantRes});
					} else {
						this.$store.commit('goods/setGoodsDetails', {goods_list: this.goods_list});
					}
					uni.navigateTo({
						url: `/plugins/result-box/index?id=${blindbox_id}&num=${goods_num}`
					});
					
				} else {
					this.getOrderResLoop(order_no);
				}
			}).catch(err => {
				uni.hideLoading();
				uni.showToast({
					title: err.msg || '查询结果失败',
					icon: 'none',
					mask: false
				});
			})
		},
		onShow() {
		},
		methods: {
			getOrderResLoop(order_no) {
				uni.showLoading({
					title: '',
					mask: true
				});
				order_result({order_no}).then(res => {
					uni.hideLoading();
					const {poolList, probabilityList, blindbox_id} = uni.getStorageSync('orderPayInfo');
					const goods_num = uni.getStorageSync('goods_num');
					const {code, data = {}} = res;
					if (code == 0) {
						const {id, detail = []} = data;
						for (let i = 0; i < detail.length; i++) {
							detail[i].goods_image = detail[i].goods_image.split(',')[0]
						}
						this.goods_list = [...detail];
						this.$store.commit('goods/setPoolsDetails', {poolList, probabilityList});
						// 筛选出赠品
						const guarantRes = detail.filter(item => {
							return item.source == 2;
						});
						if(guarantRes.length) {
							this.$store.commit('goods/setGoodsDetails', {goods_list: this.goods_list, guarantRes});
						} else {
							this.$store.commit('goods/setGoodsDetails', {goods_list: this.goods_list});
						}
						uni.navigateTo({
							url: `/plugins/result-box/index?id=${blindbox_id}&num=${goods_num}`
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.container{
	width: 100vw;
	height: 100vh;
	background: linear-gradient(90deg, #F2F0FF, #EDEBFF, #F3F8FF);

	display: flex;
	flex-direction: column;

	view:first-child{
		text-align: center;
	}

	.pay-text{
		padding-top: 50rpx;
		color: #333333;
		font-size: 32rpx;
		padding-top: 100rpx;
	}
}
</style>